


********************************************************************************
* EIA Coal Generator Level Data
********************************************************************************

*Retired Generator Data
drop _all
tempfile cumulator
quietly save `cumulator', emptyok
forval year=2014(1)2022{
	if `year' == 2014 {
		import excel "../Raw/EIA/EIA_Coal_Plant_Data/existing_gen_units_`year'.xls", clear firstrow cellrange(A2) sheet("Retired and Canceled")
		gen year = `year'
		append using `cumulator'
		quietly save `cumulator', replace
	}
	if `year' > 2014 & `year' <= 2020 {
		import excel "../Raw/EIA/EIA_Coal_Plant_Data/existing_gen_units_`year'.xlsx", clear firstrow cellrange(A2) sheet("Retired and Canceled")
		gen year = `year'
		append using `cumulator'
		quietly save `cumulator', replace
	}
	if `year' > 2020 {
		import excel "../Raw/EIA/EIA_Coal_Plant_Data/Coal Generators `year'.xlsx", clear firstrow cellrange(A2) sheet("Retired and Canceled")
		gen year = `year'
		destring UtilityID, replace force
		append using `cumulator'
		quietly save `cumulator', replace
	}
}
destring RetirementYear, gen(Retirement_Year)
collapse (max) Retirement_Year, by(UtilityID PlantCode PlantName GeneratorID)
tempfile retired_plants
save `retired_plants' //This tempfile should have retirement dates for each generator


*Active Generators in 2014-2022
drop _all
tempfile cumulator
quietly save `cumulator', emptyok
forval year=2014(1)2022{
	if `year' == 2014 {
		import excel "../Raw/EIA/EIA_Coal_Plant_Data/existing_gen_units_`year'.xls", clear firstrow cellrange(A2)
		gen year = `year'
		append using `cumulator'
		quietly save `cumulator', replace
	}
	if `year' > 2014 & `year' <= 2020 {
		import excel "../Raw/EIA/EIA_Coal_Plant_Data/existing_gen_units_`year'.xlsx", clear firstrow cellrange(A2)
		gen year = `year'
		append using `cumulator'
		quietly save `cumulator', replace
	}
	if `year' > 2020 {
		import excel "../Raw/EIA/EIA_Coal_Plant_Data/Coal Generators `year'.xlsx", clear firstrow cellrange(A2)
		gen year = `year'
		append using `cumulator'
		quietly save `cumulator', replace
	}
}

destring PlannedRetirementYear, gen(Planned_Retirement_Year)

ren UtilityName plant_company
replace plant_company = subinstr(plant_company, ".", "",.) 
replace plant_company = subinstr(plant_company, ",", "",.) 
replace plant_company = strtrim(plant_company)
replace plant_company = strupper(plant_company) 
xfill plant_company, i(UtilityID)

/*
*Balancing Data into even panels
egen eia_generator_id = group(PlantCode GeneratorID)
xtset eia_generator_id year
tsfill, full
gen no_longer_active_or_tracked = missing(UtilityID)
xfill UtilityID PlantCode PlantName OperatingYear GeneratorID plant_company, i(eia_generator_id)
*/


*Bringing in Retirement date as a factcheck
merge m:1  UtilityID PlantCode PlantName GeneratorID using `retired_plants', keep(1 3) nogen

save "../Intermediate/usa_eia_generator_data_retirement.dta", replace


use  "../Intermediate/usa_eia_generator_data_retirement.dta", clear
*Planned Variables 
egen eia_generator_id = group(PlantCode GeneratorID)
egen planned_retirement_year_earliest = min(Planned_Retirement_Year), by(eia_generator_id)
egen planned_retirement_year_latest = min(Planned_Retirement_Year), by(eia_generator_id)
egen planned_retirement_year_2017 = min(Planned_Retirement_Year) if year >= 2017, by(eia_generator_id)
gen has_plans_to_retire = !missing(Planned_Retirement_Year)
egen announced_retirement_plans = min(year) if has_plans_to_retire == 1, by(eia_generator_id)


egen tag = tag(Planned_Retirement_Year eia_generator_id)
egen eia_retire_yr_switch = total(tag), by(eia_generator_id)
by eia_generator_id (year), sort: gen eia_n_changes = sum(Planned_Retirement_Year != Planned_Retirement_Year[_n-1] & Planned_Retirement_Year != 1)
egen eia_first_plan =  min(Planned_Retirement_Year) if eia_n_changes == 1, by(eia_generator_id)
egen eia_second_plan =  min(Planned_Retirement_Year) if eia_n_changes == 2, by(eia_generator_id)
egen eia_third_plan =  min(Planned_Retirement_Year) if eia_n_changes == 3, by(eia_generator_id)
egen eia_fourth_plan =  min(Planned_Retirement_Year) if eia_n_changes == 4, by(eia_generator_id)
egen eia_fifth_plan =  min(Planned_Retirement_Year) if eia_n_changes == 5, by(eia_generator_id)


xtset eia_generator_id
xfill announced_retirement_plans
xfill planned_retirement_year_earliest




replace plant_company = "HOOSIER ENERGY RURAL ELECTRIC COOPERATIVE INC" if plant_company == "HOOSIER ENERGY R E C INC"
replace plant_company = "BASIN ELECTRIC POWER COOPERATIVE" if plant_company == "BASIN ELECTRIC POWER COOP"
replace plant_company = "BLACK HILLS CORPORATION" if plant_company == "BLACK HILLS POWER INC"
replace plant_company = "DOMINION ENERGY INC" if plant_company == "DOMINION ENERGY SOUTH CAROLINA INC"
replace plant_company = "DTE ENERGY CO" if plant_company == "DTE ELECTRIC COMPANY"
replace plant_company = "DUKE ENERGY CORPORATION" if plant_company == "DUKE ENERGY CAROLINAS LLC"
replace plant_company = "DUKE ENERGY CORPORATION" if plant_company == "DUKE ENERGY FLORIDA INC"
replace plant_company = "DUKE ENERGY CORPORATION" if plant_company == "DUKE ENERGY FLORIDA LLC"
replace plant_company = "DUKE ENERGY CORPORATION" if plant_company == "DUKE ENERGY INDIANA INC"
replace plant_company = "DUKE ENERGY CORPORATION" if plant_company == "DUKE ENERGY INDIANA LLC"
replace plant_company = "DUKE ENERGY CORPORATION" if plant_company == "DUKE ENERGY KENTUCKY INC"
replace plant_company = "DUKE ENERGY CORPORATION" if plant_company == "DUKE ENERGY OHIO INC"
replace plant_company = "DUKE ENERGY CORPORATION" if plant_company == "DUKE ENERGY PROGRESS - (NC)"
replace plant_company = "EAST KENTUCKY POWER COOPERATIVE INC" if plant_company == "EAST KENTUCKY POWER COOP INC"
replace plant_company = "EVERGY INC" if plant_company == "EVERGY KANSAS CENTRAL INC"
replace plant_company = "EVERGY INC" if plant_company == "EVERGY METRO"
replace plant_company = "FIRSTENERGY CORP" if plant_company == "FIRSTENERGY GENERATION CORP"
replace plant_company = "GULF POWER COMPANY" if plant_company == "GULF POWER CO"
replace plant_company = "HALLADOR ENERGY COMPANY" if plant_company == "HALLADOR POWER COMPANY LLC"
replace plant_company = "OTTER TAIL CORPORATION" if plant_company == "OTTER TAIL CO"
replace plant_company = "PORTLAND GENERAL ELECTRIC COMPANY" if plant_company == "PORTLAND GENERAL ELECTRIC CO"
replace plant_company = "SAN MIGUEL ELECTRIC COOPERATIVE INC" if plant_company == "SAN MIGUEL ELECTRIC COOP INC"
replace plant_company = "SOUTHERN ILLINOIS POWER CORP" if plant_company == "SOUTHERN ILLINOIS POWER COOP"
replace plant_company = "TRI-STATE GENERATION AND TRANSMISSION ASSOCIATION INC" if plant_company == "TRI-STATE G & T ASSN INC"
replace plant_company = "WESTERN FARMERS ELECTRIC COOPERATIVE" if plant_company == "WESTERN FARMERS ELEC COOP INC"
replace plant_company = "AES CORPORATION (THE)" if plant_company == "AES BEAVER VALLEY"
replace plant_company = "AES CORPORATION (THE)" if plant_company == "AES HAWAII INC"
replace plant_company = "AES CORPORATION (THE)" if plant_company == "AES HAWAII LLC"
replace plant_company = "AES CORPORATION (THE)" if plant_company == "AES SHADY POINT LLC"
replace plant_company = "AES CORPORATION (THE)" if plant_company == "AES WR LTD PARTNERSHIP"
replace plant_company = "AES CORPORATION (THE)" if plant_company == "AG PROCESSING INC"
replace plant_company = "NRG ENERGY INC" if plant_company == "NRG CHALK POINT LLC"
replace plant_company = "NRG ENERGY INC" if plant_company == "NRG ENERGY SERVICES - PLUM POINT"
replace plant_company = "NRG ENERGY INC" if plant_company == "NRG HOMER CITY SERVICES LLC"
replace plant_company = "NRG ENERGY INC" if plant_company == "NRG HUNTLEY OPERATIONS INC"
replace plant_company = "NRG ENERGY INC" if plant_company == "NRG POWER MIDWEST LP"
replace plant_company = "NRG ENERGY INC" if plant_company == "NRG REMA LLC"
replace plant_company = "NRG ENERGY INC" if plant_company == "NRG TEXAS POWER LLC"
replace plant_company = "NRG ENERGY INC" if plant_company == "NRG WHOLESALE GENERATION LP"
replace plant_company = "PPL CORP" if plant_company == "PPL MONTOUR LLC"
replace plant_company = "TALEN ENERGY CORPORATION" if plant_company == "TALEN MONTANA LLC"
replace plant_company = "VISTRA CORP" if plant_company == "VISTRA ENERGY MIAMI FORT"


rename(PlantName GeneratorID) (Plant Unit)

*Standardize Unit Names (First Pass)
replace Unit = subinstr(Unit, "GEN", "Unit ", .)
replace Unit = subinstr(Unit, "G", "Unit ", 1)
replace Unit = subinstr(Unit, "ST", "Unit ", 1)
replace Unit = subinstr(Unit, "S0", "Unit ", 1)
replace Unit = subinstr(Unit, "CO", "Unit ", 1)
replace Unit = subinstr(Unit, "PC", "Unit ", 1)
replace Unit = "Unit " + Unit if regexm(Unit, "^[0-9]+$")

*TG10 Issue
duplicates drop

replace Plant = "A. B. Brown power station" if Plant == "A B Brown"
replace Plant = "AES Hawaii Generation Plant" if Plant == "AES Hawaii"
replace Plant = "AES Warrior Run Generation Plant" if Plant == "AES Warrior Run Cogeneration Facility"
replace Plant = "Allen Fossil Plant" if Plant == "Allen"
replace Plant = "Allen S. King Generating Plant" if Plant == "Allen S King"
replace Plant = "Ames Municipal Power Plant" if Plant == "Ames Electric Services Power Plant"
replace Plant = "Antelope Valley Station" if Plant == "Antelope Valley"
replace Plant = "Apache Generating Station" if Plant == "Apache Station"
replace Plant = "Archer Daniels Midland Cedar Rapids Power Plant" if Plant == "Archer Daniels Midland Cedar Rapids"
replace Plant = "Archer Daniels Midland Clinton Power Plant" if Plant == "Archer Daniels Midland Clinton" 
replace Unit = "Unit " + Unit if Plant == "Archer Daniels Midland Clinton Power Plant"
replace Plant = "Archer Daniels Midland Decatur Power Plant" if Plant == "Archer Daniels Midland Decatur"
replace Plant = "Asbury Generating Station" if Plant == "Asbury"
replace Plant = "Asheville Plant" if Plant == "Asheville"
replace Plant = "Avon Lake power station" if Plant == "Avon Lake"
replace Plant = "Bailly Generating Station" if Plant == "Bailly"
replace Plant = "Baldwin Energy Station" if Plant == "Baldwin Energy Complex"
replace Plant = "Barry Steam Plant" if Plant == "Barry"
replace Plant = "Belews Creek Steam Station" if Plant == "Belews Creek"
replace Plant = "Belle River Power Plant" if Plant == "Belle River"
replace Plant = "Big Bend Station" if Plant == "Big Bend"
replace Plant = "Big Brown Steam Electric Station" if Plant == "Big Brown"
replace Plant = "Big Cajun II Power Plant" if Plant == "Big Cajun 2"
replace Plant = "Big Sandy Plant" if Plant == "Big Sandy"
replace Plant = "Big Stone Power Plant" if Plant == "Big Stone"
replace Plant = "Birchwood Power Facility" if Plant == "Birchwood Power"
replace Plant = "Black Dog Generating Station" if Plant == "Black Dog"
replace Plant = "Blue Valley Power Plant" if Plant == "Blue Valley"
replace Plant = "Boardman Plant" if Plant == "Boardman"
replace Plant = "Bonanza power station" if Plant == "Bonanza"
replace Plant = "Brame Energy power station" if Plant == "Brame Energy Center"
replace Plant = "Brandon Shores Generating Station" if Plant == "Brandon Shores"
replace Plant = "Brayton Point power station" if Plant == "Brayton Point"
replace Plant = "Bridgeport Harbor Station" if Plant == "Bridgeport Station"
replace Plant = "Brunner Island power station" if Plant == "Brunner Island"
replace Plant = "Bull Run Fossil Plant" if Plant == "Bull Run"
replace Plant = "Burlington Generating Station" if Plant == "Burlington (IA)"
replace Plant = "C.D. McIntosh power station" if Plant == "C D McIntosh Jr"
replace Plant = "Cambria Cogeneration Facility" if Plant == "Cambria Cogen"
replace Plant = "Cane Run Station" if Plant == "Cane Run"
replace Plant = "Carbon Power Plant" if Plant == "Carbon"
replace Plant = "Cardinal Plant" if Plant == "Cardinal"
replace Plant = "Cayuga Generating Station" if Plant == "Cayuga"
replace Plant = "Cayuga power station (New York)" if Plant == "Cayuga Operating Company"
replace Plant = "Cedar Bay Cogeneration Plant" if Plant == "Cedar Bay Generating Company LP"
replace Plant = "Chalk Point Generating Station" if Plant == "Chalk Point LLC"
replace Plant = "Chambers Cogeneration power station" if Plant == "Chambers Cogeneration LP"
replace Plant = "Charles R. Lowman Power Plant" if Plant == "Charles R Lowman"
replace Plant = "Chamois power station" if Plant == "Chamois"
replace Plant = "Cherokee Station" if Plant == "Cherokee"
replace Plant = "Chesapeake Energy Center" if Plant == "Chesapeake"
replace Plant = "Chesterfield power station" if Plant == "Chesterfield"
replace Plant = "Cheswick Power Station" if Plant == "Cheswick Power Plant"
replace Plant = "Cholla Generating Station" if Plant == "Cholla"
replace Plant = "Clifty Creek Station" if Plant == "Clifty Creek"
replace Plant = "Clinch River Plant" if Plant == "Clinch River"
replace Plant = "Clover power station" if Plant == "Clover"
replace Plant = "Coal Creek Station" if Plant == "Coal Creek"
replace Plant = "Coffeen power station" if Plant == "Coffeen"
replace Plant = "Colbert Fossil Plant" if Plant == "Colbert"
replace Plant = "Coleto Creek power station" if Plant == "Coleto Creek"
replace Plant = "Coronado Generating Station" if Plant == "Coronado"
replace Plant = "Coyote Station" if Plant == "Coyote"
replace Plant = "Crane Generating Station" if Plant == "CP Crane Power, LLC"
replace Plant = "Craig Station" if Plant == "Craig (CO)"
replace Plant = "Crist Plant" if Plant == "Crist"
replace Plant = "Cross Generating Station" if Plant == "Cross"
replace Plant = "Crystal River Energy Complex" if Plant == "Crystal River"
replace Plant = "Cumberland Steam Plant" if Plant == "Cumberland (TN)"
replace Plant = "Dale power station" if Plant == "Dale"
replace Plant = "Dallman Station" if Plant == "Dallman"
replace Plant = "Dave Johnston Power Plant" if Plant == "Dave Johnston"
replace Plant = "Dickerson Generating Station" if Plant == "Dickerson"
replace Plant = "Dolet Hills power station" if Plant == "Dolet Hills"
replace Plant = "Duck Creek Station" if Plant == "Duck Creek"
replace Plant = "Dunkirk Steam Station" if Plant == "Dunkirk Generating Plant"
replace Plant = "E.W. Brown Generating Station" if Plant == "E W Brown"
replace Plant = "Eagle Valley Station" if Plant == "Eagle Valley (IN)"
replace Plant = "East Bend Generating Station" if Plant == "East Bend"
replace Plant = "Ebensburg Generating Plant" if Plant == "Ebensburg Power"
replace Plant = "Edgecombe Genco LLC" if Plant == "Edgecombe Genco"
replace Plant = "Edgewater Generating Station" if Plant == "Edgewater"
replace Plant = "Elizabethtown Power Plant" if Plant == "Elizabethtown Power LLC"
replace Unit = "" if Plant == "Elizabethtown Power Plant"
replace Plant = "Elm Road power station" if Plant == "Elm Road Generating Station"
replace Plant = "Elmer Smith Station" if Plant == "Elmer Smith"
replace Plant = "Endicott Generating Station" if Plant == "Endicott Station"
replace Unit = "" if Plant == "Endicott Generating Station"
replace Plant = "Escalante Generating Station" if Plant == "Escalante"
replace Plant = "F. B. Culley power station" if Plant == "F B Culley"
replace Plant = "Fernandina Beach Mill power station" if Plant == "Fernandina Beach Mill"
replace Plant = "Flint Creek Power Plant" if Plant == "Flint Creek"
replace Plant = "Four Corners Steam Plant" if Plant == "Four Corners"
replace Plant = "Frank Ratts Generating Station" if Plant == "Frank E Ratts"
replace Plant = "G.G. Allen Steam Plant" if Plant == "G G Allen"
replace Plant = "Gadsden Steam Plant" if Plant == "Gadsden"
replace Plant = "Gallatin Fossil Plant" if Plant == "Gallatin (TN)"
replace Plant = "Gavin Power Plant" if Plant == "Gavin Power, LLC"
replace Plant = "Genoa Station" if Plant == "Genoa"
replace Unit = "" if Plant == "Genoa Station"
replace Plant = "George Neal Station South" if Plant == "George Neal South"
replace Plant = "Gerald Gentleman Station" if Plant == "Gerald Gentleman"
replace Plant = "Ghent Generating Station" if Plant == "Ghent"
replace Plant = "Gibbons Creek Steam Station" if Plant == "Gibbons Creek"
replace Plant = "Gibson Generating Station" if Plant == "Gibson"
replace Plant = "Glen Lyn Plant" if Plant == "Glen Lyn"
replace Plant = "Gorgas Steam Plant" if Plant == "Gorgas"
replace Plant = "Green River Generating Station" if Plant == "Green River"
replace Plant = "Greene County Steam Plant" if Plant == "Greene County"
replace Plant = "H. Wilson Sundt Generating Station" if Plant == "H Wilson Sundt Generating Station"
replace Plant = "Hammond Steam Generating Station" if Plant == "Hammond"
replace Plant = "Hardin Generating Station" if Plant == "Hardin Generator Project"
replace Unit = "Unit 1" if Plant == "Hardin Generating Station"
replace Plant = "Harding Street Station" if Plant == "Harding Street"
replace Plant = "Harllee Branch Generating Plant" if Plant == "Harllee Branch"
replace Plant = "Harrington Station" if Plant == "Harrington"
replace Plant = "Havana power station" if Plant == "Havana"
replace Plant = "Hawthorn Station" if Plant == "Hawthorn"
replace Plant = "Hayden Station" if Plant == "Hayden"
replace Plant = "Healy Power Plant" if Plant == "Healy"
replace Plant = "Hennepin power station" if Plant == "Hennepin Power Station"
replace Plant = "Herbert Wagner Generating Station" if Plant == "Herbert A Wagner"
replace Plant = "Holcomb Station" if Plant == "Holcomb"
replace Plant = "Hoot Lake Plant" if Plant == "Hoot Lake"
replace Plant = "Hugo power station" if Plant == "Hugo"
replace Plant = "Hunter Power Plant" if Plant == "Hunter"
replace Plant = "Huntington Power Plant" if Plant == "Huntington"
replace Plant = "Iatan Generating Station" if Plant == "Iatan"
replace Plant = "Independence Steam Station" if Plant == "Independence Steam Electric Station"
replace Plant = "Indian River power station" if Plant == "Indian River Generating Station"
replace Plant = "Indiantown Cogen power station" if Plant == "Indiantown Cogeneration LP"
replace Plant = "Intermountain power station" if Plant == "Intermountain Power Project"
replace Plant = "J. K. Spruce Station" if Plant == "J K Spruce"
replace Plant = "J.T. Deely Station" if Plant == "J T Deely"
replace Plant = "Jack Watson Generating Plant" if Plant == "Jack Watson"
replace Plant = "James River Cogeneration Plant" if Plant == "James River Genco LLC"
replace Plant = "James River power station" if Plant == "James River Power Station"
replace Plant = "Jim Bridger Steam Plant" if Plant == "Jim Bridger"
replace Plant = "John B. Rich Memorial Power Station" if Plant == "John B Rich Memorial Power Station"
replace Plant = "John P Madgett" if Plant == "John Madgett Station"
replace Plant = "John W. Turk Jr. Power Plant" if Plant == "John W Turk Jr Power Plant"
replace Plant = "Johnsonville Fossil Plant" if Plant == "Johnsonville"
replace Plant = "Joliet 29 Generating Station" if Plant == "Joliet 29"
replace Plant = "Joliet 9 Generating Station" if Plant == "Joliet 9"
replace Plant = "Joppa Steam Plant" if Plant == "Joppa Steam"
replace Plant = "Kammer Plant" if Plant == "Kammer"
replace Plant = "Kanawha River Plant" if Plant == "Kanawha River"
replace Plant = "Kapstone Power Plant" if Plant == "Kapstone"
replace Plant = "Keystone Generating Station" if Plant == "Keystone"
replace Plant = "Kincaid Generation LLC" if Plant == "Kincaid Generating Station"
replace Plant = "Kingston Fossil Plant" if Plant == "Kingston"
replace Plant = "Kraft Plant" if Plant == "Kraft"
replace Plant = "Kyger Creek Station" if Plant == "Kyger Creek"
replace Plant = "La Cygne Generating Station" if Plant == "La Cygne"
replace Plant = "Labadie power station" if Plant == "Labadie"
replace Plant = "Lake Road (MO) power station" if Plant == "Lake Road (MO)"
replace Plant = "Lansing power station" if Plant == "Lansing"
replace Plant = "Leland Olds Station" if Plant == "Leland Olds"
replace Plant = "Lewis & Clark Station" if Plant == "Lewis & Clark"
replace Plant = "Limestone Generating Station" if Plant == "Limestone"
replace Plant = "Lon Wright Power Plant" if Plant == "Lon Wright"
replace Plant = "Longview Plant" if Plant == "Longview Power Plant"
replace Unit = "Unit 1" if Plant == "Longview Plant"
replace Plant = "Louisa Generating Station" if Plant == "Louisa"
replace Plant = "Lumberton Power Plant" if Plant == "Lumberton"
replace Unit = "" if Plant == "Lumberton Power Plant"
replace Plant = "Major Oak power station" if Plant == "Major Oak Power"
replace Plant = "Marion Plant" if Plant == "Marion"
replace Plant = "Marshall Steam Station" if Plant == "Marshall (NC)"
replace Plant = "Martin Drake Power Plant" if Plant == "Martin Drake"
replace Plant = "Martin Lake Steam Station" if Plant == "Martin Lake"
replace Plant = "Mayo Generating Plant" if Plant == "Mayo"
replace Plant = "McIntosh Steam Plant" if Plant == "McIntosh"
replace Plant = "McMeekin Station" if Plant == "McMeekin"
replace Plant = "Mecklenburg power station" if Plant == "Mecklenburg Power Station"
replace Plant = "Meramec Power Plant" if Plant == "Meramec"
replace Plant = "Merom Generating Station" if Plant == "Merom"
replace Plant = "Merrimack Station" if Plant == "Merrimack"
replace Plant = "Miami Fort Station" if Plant == "Miami Fort"
replace Plant = "Michigan City Generating Station" if Plant == "Michigan City"
replace Plant = "Mill Creek Station" if Plant == "Mill Creek (KY)"
replace Plant = "Milton R. Young Station" if Plant == "Milton R Young"
replace Plant = "Mitchell Plant" if Plant == "Mitchell (WV)"
replace Plant = "Monroe Power Plant" if Plant == "Monroe (MI)"
replace Plant = "Monticello Steam Station" if Plant == "Monticello"
replace Plant = "Montrose Station" if Plant == "Montrose"
replace Plant = "Morgantown Energy Facility (WV)" if Plant == "Morgantown Energy Facility"
replace Plant = "Morgantown Generating Station" if Plant == "Morgantown Generating Plant"
replace Plant = "Mount Storm power station" if Plant == "Mt Storm"
replace Plant = "Muscatine Generating Station" if Plant == "Muscatine Plant #1"
replace Plant = "Muskingum River Plant" if Plant == "Muskingum River"
replace Plant = "Muskogee Generating Station" if Plant == "Muskogee"
replace Plant = "Naughton Power Plant" if Plant == "Naughton"
replace Plant = "Navajo Generating Station" if Plant == "Navajo"
replace Plant = "Nearman Creek power station" if Plant == "Nearman Creek"
replace Plant = "Nebraska City Station" if Plant == "Nebraska City"
replace Plant = "Neil Simpson Power Plants" if Plant == "Neil Simpson II"
replace Unit = "Plant II" if Plant == "Neil Simpson Power Plants"
replace Plant = "New Madrid Power Plant" if Plant == "New Madrid"
replace Plant = "Newton power station" if Plant == "Newton"
replace Plant = "North Omaha Station" if Plant == "North Omaha"
replace Plant = "Northeastern Station" if Plant == "Northeastern"
replace Plant = "Nucla Station" if Plant == "Nucla"
replace Plant = "Oak Grove Plant" if Plant == "Oak Grove (TX)" //Replace Leading O in unit James
replace Plant = "Oklaunion power station" if Plant == "Oklaunion"
replace Plant = "Ottumwa Generating Station" if Plant == "Ottumwa"
replace Plant = "Paradise Fossil Plant" if Plant == "Paradise"
replace Plant = "Pawnee Station" if Plant == "Pawnee"
replace Plant = "Philip Sporn Power Plant" if Plant == "Philip Sporn"
replace Plant = "Picway Power Plant" if Plant == "Picway"
replace Plant = "Pirkey Power Plant" if Plant == "Pirkey"
replace Plant = "Platte Generating Station" if Plant == "Platte"
replace Plant = "Pleasant Prairie Power Plant" if Plant == "Pleasant Prairie"
replace Plant = "Pleasants power station" if Plant == "Pleasants Power Station"
replace Unit = "Unit 1" if Plant == "Plum Point Energy Station"
replace Plant = "Portland Generating Station" if Plant == "Portland (PA)"
replace Plant = "Powerton Generating Station" if Plant == "Powerton"
replace Plant = "Prairie Creek Generating Station" if Plant == "Prairie Creek"
replace Plant = "Presque Isle Power Plant" if Plant == "Presque Isle"
replace Plant = "Pulliam Power Plant" if Plant == "Pulliam"
replace Plant = "Quindaro power station" if Plant == "Quindaro"
replace Plant = "Rawhide Energy Station" if Plant == "Rawhide"
replace Plant = "Ray Nixon Power Plant" if Plant == "Ray D Nixon"
replace Plant = "Reid Gardner Station" if Plant == "Reid Gardner"
replace Plant = "Rio Bravo Jasmin Power Plant" if Plant == "Rio Bravo Jasmin"
replace Unit = "" if Plant == "Rio Bravo Jasmin Power Plant"
replace Plant = "River Rouge Power Plant" if Plant == "River Rouge"
replace Plant = "River Valley power station" if Plant == "River Valley"
replace Plant = "Riverside (IA) power station" if Plant == "Riverside"
replace Plant = "Roanoke Valley Energy Facility" if Plant == "Roanoke Valley Energy Facililty I"
replace Plant = "Robert Reid Power Plant" if Plant == "Robert A Reid"
replace Plant = "Rockport Plant" if Plant == "Rockport"
replace Plant = "Roxboro Steam Plant" if Plant == "Roxboro"
replace Plant = "Rumford Cogeneration Plant" if Plant == "Rumford Cogeneration"
replace Plant = "Rush Island power station" if Plant == "Rush Island"
replace Plant = "San Juan Generating Station" if Plant == "San Juan"
replace Plant = "San Miguel Electric Cooperative" if Plant == "San Miguel"
replace Plant = "Sandow power station" if Plant == "Sandow No 4"
replace Plant = "Sandow power station" if Plant == "Sandow No 5"
replace Plant = "Sandy Creek Plant" if Plant == "Sandy Creek Energy Station"
replace Plant = "Scherer Steam Generating Station" if Plant == "Scherer"
replace Plant = "Schiller Station" if Plant == "Schiller"
replace Plant = "Scholz Generating Plant" if Plant == "Scholz"
replace Plant = "Seminole Generating Station" if Plant == "Seminole (FL)"
replace Plant = "Shawnee Fossil Plant" if Plant == "Shawnee"
replace Plant = "Shawville Generating Station" if Plant == "Shawville"
replace Plant = "Sheldon Station" if Plant == "Sheldon"
replace Plant = "Sherburne County Plant" if Plant == "Sherburne County"
replace Plant = "Shiras Station" if Plant == "Shiras"
replace Plant = "Sibley Generating Station" if Plant == "Sibley"
replace Plant = "Silver Bay Power Plant" if Plant == "Silver Bay Power"
replace Plant = "Silver Lake Plant" if Plant == "Silver Lake"
replace Plant = "Sioux Power Plant" if Plant == "Sioux"
replace Plant = "Sooner Generating Station" if Plant == "Sooner"
replace Plant = "South Oak Creek Plant" if Plant == "South Oak Creek"
replace Plant = "Springerville power station" if Plant == "Springerville"
replace Plant = "St. Clair Power Plant" if Plant == "St Clair"
replace Plant = "St. Johns River Power Park" if Plant == "St Johns River Power Park"
replace Plant = "Stanton Station" if Plant == "Stanton"
replace Plant = "Sunnyside Cogeneration Facility" if Plant == "Sunnyside Cogen Associates"
replace Plant = "Syl Laskin Energy Center" if Plant == "Syl Laskin"
replace Unit = "Unit 1" if Plant == "TS Power Plant"
replace Plant = "Tanners Creek Plant" if Plant == "Tanners Creek"
replace Plant = "Thomas Hill Energy Center" if Plant == "Thomas Hill"
replace Plant = "Tolk Station" if Plant == "Tolk"
replace Plant = "Trenton Channel Power Plant" if Plant == "Trenton Channel"
replace Plant = "Trimble County power station" if Plant == "Trimble County"
replace Plant = "Valley Power Plant" if Plant == "Valley (WI)"
replace Plant = "Valmont Station" if Plant == "Valmont"
replace Plant = "Wabash River Generating Station" if Plant == "Wabash River"
replace Plant = "Wansley Plant" if Plant == "Wansley"
replace Plant = "Warrick Power Plant" if Plant == "Warrick"
replace Plant = "Wateree Station" if Plant == "Wateree"
replace Plant = "Waukegan Generating Station" if Plant == "Waukegan"
replace Plant = "Welsh Power Plant" if Plant == "Welsh"
replace Plant = "Weston Power Plant" if Plant == "Weston"
replace Plant = "Westwood Generating Station" if Plant == "Westwood Generation LLC"
replace Plant = "White Bluff Generating Plant" if Plant == "White Bluff"
replace Plant = "Whitewater Valley Generating Station" if Plant == "Whitewater Valley"
replace Plant = "Widows Creek Fossil Plant" if Plant == "Widows Creek"
replace Plant = "Will County Generating Station" if Plant == "Will County"
replace Plant = "Williams Station" if Plant == "Williams"
replace Plant = "Winyah Generating Station" if Plant == "Winyah"
replace Plant = "Wood River Station" if Plant == "Wood River"
replace Plant = "Wygen I power station" if Plant == "Wygen 1"
replace Plant = "Wygen II power station" if Plant == "Wygen 2"
replace Unit = "" if Plant == "Wygen I power station" | Plant == "Wygen II power station"
replace Plant = "Wyodak Power Plant" if Plant == "Wyodak"
replace Plant = "Yates Steam Generating Plant" if Plant == "Yates"
replace Plant = "Yorktown power station" if Plant == "Yorktown"

replace Plant = "Campbell Generating Plant" if Plant == "J H Campbell"
replace Plant = "Boswell Energy Center" if Plant == "Clay Boswell"
replace Unit = "" if Plant == "Allen S. King Generating Plant"
replace Plant = "Amos Plant" if Plant == "John E Amos"
replace Plant = "Centralia Power Plant" if Plant == "Transalta Centralia Generation"
replace Plant = "Colstrip Steam Plant" if Plant == "Colstrip"
replace Plant = "Columbia Energy Center" if Plant == "Columbia (WI)"
replace Plant = "Colver Green Energy power station" if Plant == "Colver Green Energy"
replace Unit = "" if Plant =="Colver Green Energy power station"
replace Plant = "Comanche power station" if Plant == "Comanche (CO)"
replace Plant = "Conemaugh Generating Station" if Plant == "Conemaugh"
replace Plant = "Cooper power station" if Plant == "Cooper"
replace Plant = "Cope Station" if Plant == "Cope"
replace Plant = "Daniel Generating Plant" if Plant == "Victor J Daniel Jr"
replace Plant = "Edwardsport power station" if Plant == "Edwardsport"
replace Plant = "Fort Martin power station" if Plant == "FirstEnergy Fort Martin Power Station"
replace Unit = "Unit 1" if Unit == "Unit 01" & Plant == "Dry Fork Station"
replace Plant = "Gaston Steam Plant" if Plant == "E C Gaston"

replace Plant = "George Neal Station North" if Plant == "George Neal North"
replace Plant = "Grand River Energy Center" if Plant == "GREC"
replace Plant = "Harrison power station" if Plant == "FirstEnergy Harrison Power Station"
replace Unit = "Unit 5R" if Plant == "Hawthorn Station" & Unit  == "Unit 5"
replace Unit = "Clean Coal Unit 2" if Plant == "Healy Power Plant" & Unit  == "Unit 2"
replace Unit = subinstr(Unit, "O", "", .) if Plant == "Oak Grove Plant"
replace Plant = "John Madgett Station" if Plant == "John P Madgett"
replace Plant = "Karn Generating Plant" if Plant == "Dan E Karn"
replace Unit = "Unit 1" if  Plant == "Karn Generating Plant" & Unit == "1A"
replace Unit = "Unit 2" if  Plant == "Karn Generating Plant" & Unit == "2A"
replace Plant = "Kincaid Generating Station" if Plant == "Kincaid Generation LLC"
replace Plant = "Kimberly-Clark Chester Power Plant" if Plant == "Cope" //IDK
replace Plant = "Miller Steam Plant" if Plant == "James H Miller Jr"
replace Plant = "Montour Steam Station" if Plant == "TalenEnergy Montour"
replace Plant = "Mt. Carmel Cogeneration Facility" if Plant == "Foster Wheeler Mt Carmel Cogen"
replace Unit = subinstr(Unit, "T", "", .) if Plant == "Mt. Carmel Cogeneration Facility" 
replace Plant = "Mountaineer Plant" if Plant == "Mountaineer"
replace Plant = "North Valmy Station" if Plant == "North Valmy"
replace Plant = "Northampton Generating Plant" if Plant == "Northampton Generating Company LP"
replace Plant = "Parish Generating Station" if Plant == "W A Parish"
replace Plant = "Sikeston power station" if Plant == "Sikeston Power Station"
replace Plant = "Spiritwood Industrial Park" if Plant == "Spiritwood Station"
replace Unit = "" if Plant == "Spiritwood Industrial Park"
replace Plant = "Red Hills Generation Facility" if Plant == "Red Hills Generating Facility"
replace Unit = "Unit 1" if Plant == "Red Hills Generation Facility" & Unit == "RHUnit F"
replace Plant = "Spring Grove power station" if Plant == "P H Glatfelter"
replace Unit = "" if Plant == "Spring Grove power station" & Unit == "Unit 1" //Picking first unit
replace Plant = "Sammis Plant" if Plant == "FirstEnergy W H Sammis"
replace Plant = "Rosebud Power Plant" if Plant == "Colstrip Energy LP"
replace Plant = "Roy S. Nelson Generating Plant" if Plant == "R S Nelson" //Bug here
replace Plant = "Schahfer Generating Station" if Plant == "R M Schahfer"
replace Plant = "Seward Power Plant" if Plant == "Seward (PA)"
replace Unit = "Unit 6" if Plant == "Seward Power Plant" 
replace Plant = "Spurlock power station" if Plant == "H L Spurlock"
replace Plant = "St. Nicholas Cogeneration Plant" if Plant == "St Nicholas Cogen Project"
replace Unit = "Unit 1" if Plant == "St. Nicholas Cogeneration Plant" & Unit == "SNCP"
replace Unit = "" if Plant == "Virginia City Hybrid Energy Center"
replace Plant = "Walter Scott Jr. power station" if Plant == "Walter Scott Jr Energy Center"
replace Plant = "Wilson Station" if Plant == "D B Wilson"
replace Plant = "Wygen III power station" if Plant == "Wygen III"
replace Unit = "" if Plant == "Wygen III power station"
replace Plant = "Petersburg Generating Station" if Plant == "AES Petersburg"
replace Plant = "Plant Bowen" if Plant == "Bowen"
replace Plant = "Polk Power Station" if Plant == "Polk"
replace Unit = "Unit 1" if Plant == "Polk Power Station" & Unit == "1CA" //Picking randomly
replace Plant = "Green Station" if Plant == "R D Green"
replace Unit = "" if Plant == "Edwardsport power station" & Unit == "CT1"
replace Plant = "Prairie State Energy Campus" if Plant == "Prairie State Generating Station"
replace Unit = "Unit 1" if Plant == "Red Hills Generating Facility" & Unit == "RHUnit F"
replace Plant = "Kimberly-Clark Chester Power Plant" if Plant == "Chester Operations"
replace Plant = "Bruce Mansfield power station" if Plant == "FirstEnergy Bruce Mansfield"
replace Plant = "Cobb Generating Plant" if Plant == "B C Cobb"
replace Plant = "Conesville Power Plant" if Plant == "Conesville"
replace Plant = "Corette Plant" if Plant == "J E Corette Plant"
replace Plant = "Eastlake Power Plant" if Plant == "FirstEnergy Eastlake"
replace Plant = "Edwards Generation Plant" if Plant == "E D Edwards"
replace Plant = "Gallagher Generating Station" if Plant == "R Gallagher"
replace Plant = "Heskett Station" if Plant == "R M Heskett"
replace Plant = "Huntley power station" if Plant == "C R Huntley Generating Station" 
replace Unit = "Unit 68" if Plant == "Huntley power station" & Unit == "S68"
replace  Plant = "Hutchings Station" if Plant == "O H Hutchings"
replace Plant = "Killen Generating Station" if Plant == "Killen Station"
replace Plant = "Kline Cogeneration Facility" if Plant == "Kline Township Cogen Facility"
replace Unit = "" if Plant == "Kline Cogeneration Facility"
replace Plant = "Lansing Smith Generating Plant" if Plant == "Lansing Smith"
replace Plant = "Milton Kapp Generating Station" if Plant == "Milton L Kapp"
replace Plant = "Mitchell Steam Generating Plant (Georgia)" if Plant == "Mitchell (GA)"
replace Plant = "Wabash River Generating Station" if Plant == "Wabash River"
replace Plant = "Weadock Generating Plant" if Plant == "J C Weadock"
replace Plant = "Whiting Generating Plant" if Plant == "J R Whiting"
replace Plant = "Zimmer Generating Station" if Plant == "W H Zimmer"
replace Plant = "Beaver Valley Generation Plant" if Plant == "AES Beaver Valley Partners Beaver Valley"
replace Plant = "Lake Shore Plant" if Plant == "FirstEnergy Lake Shore"
replace Plant = "Ashtabula power station" if Plant == "FirstEnergy Ashtabula" 


//Unmatched Plants
replace Plant = "Riverton Power Plant" if Plant == "XXXX" 
replace Plant = "Stoneman Generating Station" if Plant == "XXXX"
replace Plant = "Sutherland Generating Station" if Plant == "XXXX"
replace Plant = "W.S. Lee Steam Station" if Plant == "XXXX"


keep Plant Unit announced_retirement_plans planned_retirement_year_earliest planned_retirement_year_2017 planned_retirement_year_latest eia*
collapse announced_retirement_plans planned_retirement_year_latest planned_retirement_year_earliest planned_retirement_year_2017 eia*, by(Plant Unit)
duplicates drop

rename (announced_retirement_plans planned_retirement_year_earliest) (eia_announce_retire_yr  eia_retire_yr_plan)

gen eia_plan_dif_1 = eia_second_plan - eia_first_plan
gen eia_plan_dif_2 = eia_third_plan - eia_second_plan
gen eia_plan_dif_3 = eia_fourth_plan - eia_third_plan
gen eia_plan_dif_4 = eia_fifth_plan - eia_fourth_plan
gen eia = 1


save "../Intermediate/usa_eia_generator_data.dta", replace



